
const url = 'https://oa.wangdian.cn/api/';//线上
//const url = 'http://www.test3.com/api/'; //本地
const email = 'sys@wangdian.cn'; //账号
const password = 'Sys1234@'; //密码
var refresh_token = '';

var count_error = 0;
/**
 * 刷新token
 * @param {int} refreshtime 刷新时间:ms
 * @returns {null}
 */

function refreshToken(refreshtime) {
    var refresh = setTimeout(function () {
        $.ajax({
            url: url + 'get_passport_token',
            type: 'POST',
            dataType: 'json',
            data: {refreshToken: refresh_token, grantType: 'refresh_token'},
            success: function (res) {
                if (res.code === 0) {
                    //添加ajax全局请求头
                    $.ajaxSetup({
                        headers: {
                            'Authorization': 'Bearer ' + res.data.access_token
                        }
                    });
                    refresh_token = res.data.refresh_token;
                    refresh();
                } else {
                    alert('登录异常，请联系管理员');
                }
            },
            error: function (e) {
                count_error++;
                if (count_error < 5) {
                    refresh();
                } else {
                    alert('服务器连接失败，请刷新页面重试');
                }
                console.log(e);
            }
        });
    }, refreshtime);
}

/**
 * 主逻辑
 */
$(document).ready(function () {

    //定时刷新token（刷新时间:ms）
    refreshToken(1000 * 60 * 110);

    //首次请求加载所有页面初始化数据

    var group_rank_all = [];
    var solo_rank_all = [];
    $.ajax({
        url: url + 'get_passport_token',
        type: 'POST',
        dataType: 'json',
        data: {email: email, password: password, grantType: 'password'},
        success: function (res) {
            if (res.code === 0) {
                //添加ajax全局请求头
                $.ajaxSetup({
                    headers: {
                        'Authorization': 'Bearer ' + res.data.access_token
                    }
                });
                refresh_token = res.data.refresh_token;
                //第一次请求排行榜并加载数据
                $.ajax({
                    url: url + 'tv/rank/store',
                    type: 'POST',
                    dataType: 'json',
                    success: function (res) {
//                        console.log(res);
                        if (res.code === 0) {
                            var data = res.data;
                            var group_rank = data.team;
                            var solo_rank = data.solo;
                            var group_ul = '';
                            var solo_ul = '';
                            group_rank_all = group_rank;
                            solo_rank_all = solo_rank;
//                            console.log(group_rank_all);
                            for (let i = 0; i < group_rank.length; i++) {
                                group_ul += '<li id="group' + group_rank[i].team_id + '"><p><span>' + (i + 1) + '</span><span>' + group_rank[i].name + '</span><span>' + group_rank[i].returned + '</span></p></li>';
                            }
                            for (let i = 0; i < solo_rank.length; i++) {
                                solo_ul += '<li id="solo' + solo_rank[i].team_id + '"><p><span>' + (i + 1) + '</span><span>' + solo_rank[i].name + '</span><span>' + solo_rank[i].returned + '</span></p></li>';
                            }
                            $('#group').html(group_ul);
                            $('#solo').html(solo_ul);

                            loadData([group_rank_all, solo_rank_all], 5000, 1500, 1.75);
                        }
                    }
                });
                //弹幕获取初始时间并添加刷新定时器
                $.ajax({
                    url: url + 'tv/rank/bullet_screen',
                    type: 'POST',
                    dataType: 'json',
                    success: function (rs) {
                        console.log(rs);
                        if (rs.code === 0) {
                            var time = rs.data.time;
                            loadBarrage(time);
                        }
                    }
                });

                /**
                 * 兵王榜
                 */
                var king = [
                    {name: '一月 孙丽艳', ico: 'img/king/tmp/photo.jpg'},
                    {name: '二月 孙丽艳', ico: 'img/king/tmp/photo.jpg'},
                    {name: '三月 孙丽艳', ico: 'img/king/tmp/photo.jpg'},
                    {name: '四月 孙丽艳', ico: 'img/king/tmp/photo.jpg'},
                    {name: '五月 孙丽艳', ico: 'img/king/tmp/photo.jpg'}
                ];//取数据
                //dom
                var ul_html = '';
                for (var i = 0; i < king.length; i++) {
                    var rotate = '';
                    switch (i) {
                        case 0:
                            rotate = 'rotate_to_left';
                            break;
                        case 1:
                            rotate = 'rotate_to_left';
                            break;
                        case 2:
                            rotate = 'rotate_to_mid';
                            break;
                        case 3:
                            rotate = 'rotate_to_right';
                            break;
                        case 4:
                            rotate = 'rotate_to_right';
                            break;
                    }
                    ul_html += '<li id=king' + (i + 1) + ' class="' + rotate + '"><div><img src="' + king[i].ico + '" alt=""><p class="wordart">' + king[i].name + '</p></div></li>';
                }
                $('#rotate ul').html(ul_html);
                //轮播
                var transform_index = 1;
                setInterval(function () {
                    if (transform_index > 5) {
                        transform_index = 1;
                    }
                    transformKing(transform_index);
                    transform_index++;
                }, 3000);



            } else {
                alert('账号或密码错误');
            }
        }
    });
    //菜单显示隐藏
    $('#menu_trigger').mouseenter(function () {
        $('#menu').animate({bottom: 0}, 500);
    });
    $('#hidemenu').click(function () {
        $('#menu').animate({bottom: '-11.13rem'}, 500);
    });

    $('#menu_to_rank div,#menu_to_rank span').click(function () {
        $('#body_rank').show();
        $('#body_pk').hide();
        $('#region').hide();
        $('#body_king').hide();
        $('#hidemenu').click();
    });
    $('#menu_to_pk div,#menu_to_pk span').click(function () {
        $('#body_pk').show();
        $('#body_rank').hide();
        $('#region').hide();
        $('#body_king').hide();
        $('#hidemenu').click();
    });
    $('#menu_to_team div,#menu_to_team span').click(function () {
        $('#region').show();
        $('#body_rank').hide();
        $('#body_pk').hide();
        $('#body_king').hide();
        $('#hidemenu').click();
    });
    $('#menu_to_king div,#menu_to_king span').click(function () {
        $('#body_king').show();
        $('#region').hide();
        $('#body_rank').hide();
        $('#body_pk').hide();
        $('#hidemenu').click();
    });
});
